import { checkLimitSize } from '@/utils/appHead';
export default {
	props: {
		//是否显示loading
		loading: {
			type: Boolean,
			default: true,
		},

		//辅助提示
		tips: {
			type: String,
			default: '',
		},

		//文件大小限制，默认5M
		limitSize: {
			type: Number,
			default: 5,
		},

		//必选参数，上传的地址
		action: {
			type: String,
			default: 'https://jsonplaceholder.typicode.com/posts/',
		},

		//上传的文件字段名
		name: {
			type: String,
			default: 'file',
		},

		//接受上传的文件类型  https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input#attr-accept
		accept: {
			type: String,
		},

		//是否禁用
		disabled: {
			type: Boolean,
			default: false,
		},
	},
	data() {
		return {
			fullLoading: false,
		};
	},

	methods: {
		//上传文件之前的钩子，参数为上传的文件，若返回 false 或者返回 Promise 且被 reject，则停止上传
		beforeFileUpload(file) {
			let isPass = checkLimitSize(file, this.limitSize);
			if (!isPass) {
				this.$notify.warning({
					title: '操作失败',
					message: `文件大小超出${this.limitSize}M`,
				});
			}

			if (this.loading) {
				this.fullLoading = true;
			}
			return isPass;
		},

		//当文件上传网络出错时触发
		onFileError() {
			this.$notify.error({
				title: '操作失败',
				message: '文件上传出错',
			});
			this.fullLoading = false;
		},

		//当文件数量超出限制时触发
		onFileOverLimit() {
			this.$notify({
				title: '操作失败',
				message: `文件最多只能传${this.limit}个哦`,
				type: 'warning',
			});
		},
	},
};
